package com.atguigu.teach.learns;

import sun.security.krb5.internal.Ticket;

import java.util.concurrent.locks.ReentrantLock;

// 第一步 先new资源类
class Ticket2{
    private int num=50;
    private final ReentrantLock lock=new ReentrantLock();
    public  void sale(){
        lock.lock();
        try {
            if(num >0){
                System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+ (num--)+"剩下:"+num);
             }
          } catch (Exception e) {
                    e.printStackTrace();
                }finally {
            lock.unlock();
        }

    }
}





public class SalesTicketDemo {

    public static void main(String[] args) {
        Ticket2 ticket = new Ticket2();
       new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<51;i++) {
                    ticket.sale();
                }
            }
        },"a").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<51;i++) {
                    ticket.sale();
                }
            }
        },"b").start();


       new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<51;i++) {
                    ticket.sale();
                }
            }
        },"c").start();


}
}

